/*
	IsoMetric 3D Tile Designs
*/
package
{
	import as3isolib.display.IsoSprite;
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	import as3isolib.graphics.Stroke;
	
	import com.greensock.*;
	
	import eDpLib.events.ProxyEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.osmf.elements.ProxyElement;
	
	[SWF(width="800", height="600", backgroundColor="#000000", frameRate="30")]
	public class Isometric3dIntroduction extends Sprite
	{
		private static const CELL_SIZE:Number = 50;
		
		private var grid:IsoGrid;
		private var scene:IsoScene;
		private var view:IsoView;
		private var box:IsoBox;
		private var iso:IsoSprite;
		
		public function Isometric3dIntroduction()
		{
			grid 			= new IsoGrid();
			//grid.addEventListener(MouseEvent.CLICK, gridClick);
			//grid.addEventListener(MouseEvent.MOUSE_MOVE, gridClick);
			grid.setGridSize(8, 8, 1);
			grid.gridlines	= new Stroke(1, 0xcccccc);
			grid.showOrigin	= false;
			grid.cellSize	= CELL_SIZE;
					
			//box				= new IsoBox();
			//box.setSize(CELL_SIZE, CELL_SIZE, 10);
			//box.moveTo(CELL_SIZE, CELL_SIZE, 0);
			
			scene 			= new IsoScene();
			//scene.addChild(grid);
			//scene.addChild(box);
			
			for(var i:int=0; i<8; i++)
			{
				for(var j:int=0; j<8; j++)
				{
					iso				= new IsoSprite();
					iso.addEventListener(MouseEvent.ROLL_OVER, isoOver);
					if(Math.random() > 0.5)
					{
						iso.sprites		= [brick];						
						iso.moveBy(int(j*50)-25, int(i*50)+25, 0);
					}else{
						
						iso.sprites		= [grass];						
						iso.moveBy(j*50, i*50, 0);
					}
					scene.addChild(iso);
				}
			}
			
			
			scene.render();
			
			view			= new IsoView();
			view.setSize(800, 600);
			view.centerOnPt(new Pt(150, 150, 0));
			view.addScene(scene);
			addChild(view);
			
			
			addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function loop(e:Event):void
		{
			scene.render();
		}
		
		private function isoOver(e:ProxyEvent):void
		{
			var iso:IsoSprite	= e.proxy as IsoSprite;
			TweenMax.to(iso, 0.3, {z:100, yoyo:true, repeat:1, startAt:{z:0}});
		}
		
		private function gridClick(event:ProxyEvent):void
		{
			var me:MouseEvent	= MouseEvent(event.targetEvent);
			var p:Pt			= new Pt(me.localX, me.localY);
			IsoMath.screenToIso(p);
			//box.moveTo(Math.floor(p.x/CELL_SIZE)*CELL_SIZE, Math.floor(p.y/CELL_SIZE)*CELL_SIZE, 0);
			scene.render();			
		}
	}
}